package com.odais.AE_Planner;

import org.andengine.engine.Engine;
import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.entity.Entity;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;

import android.content.Context;
import android.util.Log;

/**
 *
 * @author Lionel DECELLE
 * @since 8 juin 2012 - 10:51:18
 *
 */
public class RoomObject extends Entity
{
// ===========================================================
// Constants
// ===========================================================


// ===========================================================
// Fields
// ===========================================================
	private Context mContext;
	private Engine mEngine;
	private Scene mScene;
	private String filePNG;
	private float mPosX, mPosY;
	private Sprite sprite;

	private BitmapTextureAtlas mBitmapTextureAtlas;
	private ITextureRegion mFaceTextureRegion;

// ===========================================================
// Constructors
// ===========================================================
	public RoomObject(SceneContext sceneContext, String filePNG, float mPosX, float mPosY) {
		this.mContext = sceneContext.getContext();
		this.mEngine = sceneContext.getEngine();
		this.mScene = sceneContext.getScene();
		this.mPosX = mPosX;
		this.mPosY = mPosY;
		this.filePNG = filePNG;

		if(this.filePNG != null)
		{
			this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.mEngine.getTextureManager(), 64, 64, TextureOptions.BILINEAR);
		    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
			this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this.mContext, this.filePNG, 0, 0);
			this.mBitmapTextureAtlas.load();
	
	
	
		    float x = this.mPosX;
		    float y = this.mPosY;
	
		    sprite = new Sprite(x, y, this.mFaceTextureRegion, this.mEngine.getVertexBufferObjectManager());
	
		    PhysicsHandler physicsHandler = new PhysicsHandler(sprite);
		    sprite.registerUpdateHandler(physicsHandler);
	
		    this.mScene.attachChild(sprite);
		}
	}

// ===========================================================
// Getter & Setter
// ===========================================================
	public Sprite getSprite() {
			return sprite;
		}

	public float getX() {
		return sprite.getX();
	}

	public float getY() {
		return sprite.getY();
	}

	public float getHeight() {
		return sprite.getHeight();
	}

	public float getWidth() {
		return sprite.getWidth();
	}


	public void kill()
	{
	    this.mScene.detachChild(sprite);
	    if (this != null) {
            try {
                this.clearEntityModifiers();
                this.clearUpdateHandlers();
//                if (this instanceof this.mScene) {
//                    ((Scene) this).clearTouchAreas();
//                }
                this.detachSelf();
                this.detachChildren();
                this.setIgnoreUpdate(true);
                this.setVisible(false);
            } catch (Exception e) {
                Log.d("Odais Killer", "Entity doesn't want to die!");
                e.printStackTrace();
            }
        }
	}

}